from rest_framework.generics import ListAPIView, RetrieveAPIView

from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import OrderingFilter
from rest_framework.pagination import PageNumberPagination

from . import models
from . import serializers


# Create your views here.

class CourseCategoryAPIView(ListAPIView):
    queryset = models.CourseCategory.objects.filter(is_show=True, is_deleted=False).order_by('orders')
    serializer_class = serializers.CourseCategoryModelSerializer


# 为实现分页功能创建的类
class CustomPageNumberPagination(PageNumberPagination):
    # page_query_param = 'pp'  # 指定进行分页使用的参数，这里默认使用page参数进行设置
    page_size = 5   # 默认每页显示5条数据
    page_size_query_param = 'size'  # 可以根据size参数调整每页显示的数据条数
    max_page_size = 20  # 每页最多显示20条


class CourseListAPIView(ListAPIView):
    queryset = models.Course.objects.filter(is_show=True, is_deleted=False).order_by('orders')
    serializer_class = serializers.CourseModelSerializer
    filter_backends = [DjangoFilterBackend, OrderingFilter]   # 增加筛选、过滤功能
    filter_fields = ['course_category', ]   # 使用此字段进行筛选
    ordering_fields = ['id', 'students', 'price']   # 使用这些字段进行排序。
    pagination_class = CustomPageNumberPagination   # 分页功能


class CourseDetailAPIView(RetrieveAPIView):
    queryset = models.Course.objects.filter(is_show=True, is_deleted=False).order_by('orders')
    serializer_class = serializers.CourseDetailModelSerializer
